package {
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.*;
	import flash.media.Sound;
	import flash.media.Video;
	import flash.net.NetConnection;
	import flash.net.NetStream;
	import flash.net.URLRequest;
	import flash.text.TextField;
	import flash.text.TextFormat;

	
	public class Media extends Sprite {
		private var type:String;
		private var id:Number;
		private var dateTime:Date;
		private var latitude:Number;
		private var longitude:Number;
		private var altitude:Number;
		private var text:String;
		private var url:String;
		private var annotation:String;
		private var sound:Sound;
		
		public function Media(type:String, id:Number, time_captured:String, latitude:Number,
							  longitude:Number, altitude:Number, url:String, annot:String) {
			this.type = type;
			this.id = id;
			this.dateTime = parseDate(time_captured);
			this.latitude = latitude;
			this.longitude = longitude;
			this.altitude = altitude;
			this.text = url;
			this.url = Common.URL_PHOTO_START + url;
			this.annotation = annot;
		}
		
		/**
		 * Draws the appropriate media type
		 */
		public function drawMedia(mediaContainer:Sprite):Sprite {
			if(type == Common.M_PHOTO) {
				mediaContainer = drawPicture(mediaContainer);
			} else if (type == Common.M_NOTE) {
				mediaContainer = drawNote(mediaContainer);
			} else if (type == Common.M_AUDIO) {
				mediaContainer = drawAudio(mediaContainer);
			}
			
			return mediaContainer;
		}
		
		/**
		 * @return
		 * width of current media
		 */
		public function getMediaWidth():int {
			var width:int = Common.THUMB_WIDTH;	// default width
			
			if(type == Common.M_PHOTO) {
				return Common.THUMB_WIDTH;
			} else if (type == Common.M_NOTE) {
				return Common.NOTE_WIDTH;
			} else if (type == Common.M_AUDIO) {
				return Common.AUDIO_WIDTH;
			}
			
			return width;
		}
		
		/**
		 * Draws a single picture for display in the main media stream
		 */
		private function drawPicture(pic:Sprite):Sprite {
			// load images from server
			var urlRequest:URLRequest = new URLRequest(this.url);
			var loader:Loader = new Loader();
			loader.load(urlRequest);
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoadComplete);
			
			pic.addChild(loader);
			
			// picture characteristics				
			//	pic.buttonMode = true;
			
			return pic;
		}
		
		/**
		 * Adjusts properties of the Loader (which holds our picture)
		 * after the loading is complete
		 */
		private function imageLoadComplete(event:Event):void {
			var loader:Loader = Loader(event.target.loader);
			//	loader.width = Common.THUMB_WIDTH;
			//	loader.height = Common.THUMB_HEIGHT;
			var picWidth:Number = loader.width;
			var picHeight:Number = loader.height;
			var scaler:Number;
			
			// vertically oriented pictures
			if (picHeight > picWidth) {
				scaler = picHeight / Common.THUMB_HEIGHT;
			} else { // horizontally oriented pictures
				scaler = picWidth / Common.THUMB_WIDTH;
			}
			
			// shrink the picture to fit as a thumbnail
			loader.height = picHeight / scaler;
			loader.width = picWidth / scaler;
			//	loader.x = Common.THUMB_WIDTH/2 - loader.width/2;
			//	loader.y = Common.THUMB_HEIGHT/2 - loader.height/2;
		}
		
		/**
		 * Draws a single note for display in the main media stream
		 */
		private function drawNote(noteContainer:Sprite):Sprite {
			var noteWidth:int = Common.NOTE_WIDTH;
			var noteHeight:int = Common.NOTE_HEIGHT;
			
			noteContainer.graphics.lineStyle(1, 0xBBBBBB);
			noteContainer.graphics.beginFill(0xFFFFFF, 1.0);
			noteContainer.graphics.drawRect(0, 0, noteWidth, noteHeight);
			noteContainer.graphics.endFill();
			
			// red margin line
			noteContainer.graphics.lineStyle(1, 0xFF9999);
			noteContainer.graphics.moveTo(noteWidth/6, 0);
			noteContainer.graphics.lineTo(noteWidth/6, noteHeight);
			
			// blue ruled lines
			noteContainer.graphics.lineStyle(1, 0xBBBBFF);
			for(var y:int = noteHeight/8; y < noteHeight; y += noteHeight/15) {
				noteContainer.graphics.moveTo(0, y);
				noteContainer.graphics.lineTo(noteWidth, y);
			}
			
			// generic note header text
			var textStyle:TextFormat = new TextFormat();
			textStyle.font = "Calibri";
			textStyle.bold = true;
			textStyle.size = 18;
			textStyle.align = "left";
			
			var myText:TextField = new TextField();
			myText.x = noteWidth/6 + 10;
			myText.y = 10;
			myText.width = noteWidth - myText.x;
			myText.height = noteHeight - myText.y;
			myText.text = "Notes from the road";
			myText.setTextFormat(textStyle);
			noteContainer.addChild(myText);
			
			// note text
			textStyle = new TextFormat();
			textStyle.font = "Calibri";
			textStyle.size = 16;
			textStyle.align = "left";
			
			myText = new TextField();
			myText.x = noteWidth/6 + 10;
			myText.y = noteHeight/8;
			myText.width = noteWidth - myText.x;
			myText.height = noteHeight - myText.y;
			myText.wordWrap = true;
			myText.text = this.text;
			myText.setTextFormat(textStyle);
			noteContainer.addChild(myText);
			
			return noteContainer;
		}
		
//		var video:Video = new Video();
//		var nc:NetConnection = new NetConnection();
//		var ns:NetStream = new NetStream(nc);
		
		/**
		 * Draws a single audio block for display in the main media stream
		 */
		private function drawAudio(container:Sprite):Sprite {
			var width:int = Common.AUDIO_WIDTH;
			var height:int = Common.AUDIO_HEIGHT;
			
			// graphical container
//			container.graphics.lineStyle(1, 0xBBBBBB);
			container.graphics.beginFill(0x000000, 1.0);
			container.graphics.drawRoundRect(0, 0, width, height, 30);
			container.graphics.endFill();
			
			// load the audio
			sound = new Sound();
//			sound.load(new URLRequest(url));
//			trace(url);
//			sound.addEventListener(Event.COMPLETE, soundLoadComplete); 
			
//			var vid:Video = new Video();
//			
//			var nc:NetConnection = new NetConnection();
//			nc.connect(null);
//			
//			var ns:NetStream = new NetStream(nc);
//			
//			ns.play("http://www.helpexamples.com/flash/video/cuepoints.flv");
//			
//			addChild(vid);
//			vid.attachNetStream(ns);

			VideoExample();
			
			return container;
		}
		
		//*******************************
		
		private function VideoExample():void {
			var video:Video = new Video();
			addChild(video);
			
			var nc:NetConnection = new NetConnection();
			nc.connect(null);
			
			var ns:NetStream = new NetStream(nc);
			ns.client = {onMetaData:ns_onMetaData, onCuePoint:ns_onCuePoint};
			
			video.attachNetStream(ns);
			trace(url);
			ns.play(url);
		}
		
		
		function ns_onMetaData(item:Object):void {
			trace("metaData");
			// Resize video instance.
		}
		
		function ns_onCuePoint(item:Object):void {
			trace("cuePoint");
		}
		
		
		//*********************************
		
		
		private function soundLoadComplete(event:Event):void {
			trace("sound!");
			sound.play();
		}
		
		public function parseDate(s:String):Date {
			var year:String = s.substring(0, 4);
			
			// Flash treats months as numbers from 0-11 (instead of 1-12)
			// for god knows what reason
			var month:int = int(s.substring(5, 7)) - 1;
			var day:String = s.substring(8, 10);
			var hours:String = s.substring(11, 13);
			var minutes:String = s.substring(14, 16);
			
			return new Date(year, month, day, hours, minutes, null);
		}
		
		public function getType():String {
			return type;
		}
		
		public function getId():Number {
			return id;
		}
		
		public function getDate():Date {
			return dateTime;
		}
		
		public function getLatitude():Number {
			return latitude;
		}
		
		public function getLongitude():Number {
			return longitude;
		}		
		
		public function getAltitude():Number {
			return altitude;
		}
		
		public function getText():String {
			return text;
		}
		
		public function getUrl():String {
			return url;
		}
		
		public function setAnnotation(newAnnot:String):void {
			annotation = newAnnot;
		}
		
		public function getAnnotation():String {
			return annotation;
		}
	}
}